package com.summer.controller;


import com.auth0.jwt.exceptions.TokenExpiredException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.summer.entity.TbFeedback;
import com.summer.entity.TbRole;
import com.summer.entity.TbUserinfo;
import com.summer.mapper.TbFeedbackMapper;
import com.summer.mapper.TbRoleMapper;
import com.summer.mapper.TbUserinfoMapper;
import com.summer.utils.JwtToken;
import com.summer.utils.MyUtil;
import com.summer.utils.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器未测试
 * </p>
 * 测试完成20:34
 * @author summer502
 * @since 2021-03-31
 */
@CrossOrigin
@RestController
@RequestMapping("/tb-feedback")
@Transactional
@Api(value = "TbFeedbackController", description = "反馈api")

public class TbFeedbackController {
    Boolean flag;
    MyUtil myUtil = new MyUtil();
    @Autowired(required =false)
    private TbFeedbackMapper tbFeedbackMapper;
    HttpServletRequest request;

    /**
     * 超级管理员添加角色(可以有普通用户和协会组织和教师)"
     *
     * @param Authorization
     * @return
     * @throws IOException
     * @throws TokenExpiredException
     */
    @ApiOperation("添加反馈")
    @PostMapping("/TbFeedback/addTbFeedback")
    @ResponseBody
    public ResponseEntity addTbFeedback(
                                     @RequestParam(required = true)int activityId,
                                    // @RequestParam(required = false)int user_id,
                                     @RequestParam(required = true)String feedbackContent,
                                     @RequestHeader String Authorization
    ) throws IOException, TokenExpiredException {
        ResponseEntity responseEntity;
        String token = Authorization;
        //parentRoleId
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo tbUserinfo = JwtToken.unsign(token, TbUserinfo.class);
        responseEntity = ResponseEntity.ok();
        TbFeedback tbFeedback = new TbFeedback();
        tbFeedback.setFeedbackContent(feedbackContent);
        tbFeedback.setActivityId(activityId);
        tbFeedback.setUserId(tbUserinfo.getUserId());
        tbFeedbackMapper.insert(tbFeedback);
        responseEntity.putDataValue("feedback_id",tbFeedback.getFeedbackId());
        responseEntity.putDataValue("msg","添加反馈成功");
        return responseEntity;
    }

    /**
     * 修改反馈内容
     * @param feedbackId
     *
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("修改反馈内容")
    @PutMapping("/web/updateTbFeedback")
    @ResponseBody
    public ResponseEntity updateTbFeedback(@RequestParam(required = true) int feedbackId,
                                        @RequestParam(required = true) String feedbackContent,
                                         @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users = JwtToken.unsign(token, TbUserinfo.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else {
            responseEntity = ResponseEntity.ok();
            TbFeedback tbFeedback = tbFeedbackMapper.selectById(feedbackId);
            tbFeedback.setFeedbackContent(feedbackContent);
            tbFeedbackMapper.updateById(tbFeedback);
            responseEntity.putDataValue("msg", "反馈修改成功");
            return responseEntity;
        }
    }

    /**
     * 根据feedbackId查询反馈
     * @param feedbackId
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据feedbackId查询反馈")
    @PutMapping("/web/selectByTbFeedbackId")
    @ResponseBody
    public ResponseEntity selectByTbFeedbackId(@RequestParam(required = true) Integer feedbackId, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users = JwtToken.unsign(token, TbUserinfo.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        }
            responseEntity=ResponseEntity.ok();
            TbFeedback tbFeedback = tbFeedbackMapper.selectById(feedbackId);
            responseEntity.putDataValue("tbFeedback",tbFeedback);
            return responseEntity;
    }

    /**
     * 根据id删除反馈
     * @param feedbackId
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据id删除反馈")
    @PutMapping("/web/deleteTbFeedback")
    @ResponseBody
    public ResponseEntity deleteTbFeedback(@RequestParam(required = true) Integer feedbackId,@RequestHeader String Authorization)throws IOException{
        ResponseEntity responseEntity;
        String token=Authorization;
        if(flag=myUtil.judgeToken(token)){
            responseEntity=ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users=JwtToken.unsign(token,TbUserinfo.class);
        if(users==null){
            responseEntity=ResponseEntity.notFound();
            responseEntity.putDataValue("msg","用户不存在");
            return responseEntity;
        }else if(users.getRoleId()==1) {
            TbFeedback tbFeedback = tbFeedbackMapper.selectById(feedbackId);
            tbFeedback.setIsDelete(1);
            tbFeedbackMapper.updateById(tbFeedback);
            responseEntity=ResponseEntity.ok();
            responseEntity.putDataValue("msg","删除成功");
            return responseEntity;
            }

        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }
    /**
     * 反馈列表
     * @param Authorization
     * @param response
     * @return
     * @throws IOException
     */
    @ApiOperation("反馈列表")
    @PostMapping("/web/TbFeedbacksList")
    @ResponseBody
    public ResponseEntity TbFeedbackList(@RequestHeader String Authorization,
                                       HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        /*星号表示所有的域都可以接受，*/
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");

        ResponseEntity responseData;
        String token = Authorization;
        //通过token解密出来的user对象
        TbUserinfo user = JwtToken.unsign(token, TbUserinfo.class);
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseData = ResponseEntity.forbidden();
            responseData.putDataValue("msg","token不存在或已过期");
            return responseData;
        }
        if (user.getRoleId()!=1){
            responseData = ResponseEntity.badRequest();
            responseData.putDataValue("msg","权限错误");
            responseData.putDataValue("timestamp",myUtil.getTime());
            return responseData;
        }
        QueryWrapper<TbFeedback> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<TbFeedback> list = tbFeedbackMapper.selectList(wrapper);
        responseData = ResponseEntity.ok();
        responseData.putDataValue("list",list);
        responseData.putDataValue("msg","成功");
        return responseData;
    }
    /**
     * 统计反馈数量
     */
    @ApiOperation("统计反馈数量")
    @GetMapping("/web/count")
    @ResponseBody
    public ResponseEntity count() throws IOException {
        ResponseEntity responseData;
        int is_delete = 0;
        QueryWrapper<TbFeedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", is_delete);
        int  count =tbFeedbackMapper.selectCount(queryWrapper);
        responseData = ResponseEntity.ok();
        responseData.putDataValue("count", count);
        return responseData;
    }
}
